package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-05-24
 * Time: 20:47
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");


        int currentSize = bookList.getUsedSize();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书");
        String name = scanner.nextLine();

        int pos = 0;
        int i = 0;
        for (; i < currentSize; i++) {
            //遍历，书架，已初始化的书
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                //找到要删除的，位置
                pos = i;
                break;
            }
        }

        if (i == currentSize) {
            System.out.println("没有找到你要删除的图书");
        }

        //开始删除
        for (int j = pos; j < currentSize-1; j++) {
         //思路:bookList[j] = bookList[j+1];
            //先找到j+1,那个位置，然后覆盖
            Book book = bookList.getBook(j+1);
            bookList.setBook(j, book);
        }

        //更新下标
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
